package com.sne.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sne.common.entity.Result;
import com.sne.common.enums.ResultCode;
import com.sne.sys.dto.SysUserDTO;
import com.sne.sys.dto.SysUserParamDTO;
import com.sne.sys.entity.SysUser;
import com.sne.sys.service.ISysUserService;
import com.sne.sys.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author fanghh
 * @since 2020-01-03
 */
@RestController
@RequestMapping("/sys/sys-user")
@Api(tags = "用户管理")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/info")
    public Result info() {
        return Result.success(iSysUserService.getCurrentUserInfo());
    }

    @ApiOperation(value = "根据用户名获取用户")
    @GetMapping("/findByUserName/{userName}")
    public Result findByUserName(@PathVariable("userName") String userName) {
        SysUserVO userVO = iSysUserService.findByName(userName);
        return Result.success(userVO);
    }

    @ApiOperation(value = "注册用户")
    @PostMapping("/sign")
    public Result sign(SysUser user) {
        if (StringUtils.isAnyBlank(user.getUserName(), user.getPassword())) {
            return Result.failed(ResultCode.PARAM_INVALID);
        }
        iSysUserService.register(user);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('sys:user:page')")
    @ApiOperation(value = "用户列表")
    @PostMapping("/page")
    public Result page(Page page,@RequestBody SysUserParamDTO paramDTO) {
        return Result.success(iSysUserService.findPage(page, paramDTO));
    }

    @PreAuthorize("hasAuthority('sys:user:save')")
    @ApiOperation(value = "保存用户")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SysUserDTO userDTO) {
        return Result.success(iSysUserService.saveUser(userDTO));
    }

    @PreAuthorize("hasAuthority('sys:user:update')")
    @ApiOperation(value = "更新用户")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody SysUserDTO userDTO) {
        return Result.success(iSysUserService.updateUser(userDTO));
    }

    @ApiOperation(value = "根据用户id获取用户")
    @GetMapping("/find/{id}")
    public Result find(@PathVariable("id") Long id) {
        return Result.success(iSysUserService.findById(id));
    }

    @PreAuthorize("hasAuthority('sys:user:delete')")
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(iSysUserService.deleteUser(id));
    }

    @PreAuthorize("hasAuthority('sys:user:deleteIds')")
    @ApiOperation(value = "批量删除用户")
    @DeleteMapping("/deleteIds")
    public Result deleteIds(@RequestBody @Size(min = 1,message = "ids不能为空") List<Long> ids) {
        return Result.success(iSysUserService.deleteIds(ids));
    }

}

